package com.chouren.goandroid.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.chouren.goandroid.R;

public class RotaryView extends View {
	private Bitmap bitmapScale;
	private Paint bitmapPaint;
	private Paint anglePaint;
	private Paint arcPaint;
	private RectF rectf;
	private int sweepAngle;
	private int startAngle;
	
	private final int[] ARC_COLORS = new int[] { 0xa03cbeff, 0xa0ff5e7d,
			0xa04cd964, 0xa0ffc71e, 0xa0ff6f2f };

	public RotaryView(Context context) {
		super(context);
		init();
	}

	private void init() {
		arcPaint = new Paint();
		arcPaint.setAntiAlias(true);
		arcPaint.setStyle(Paint.Style.FILL_AND_STROKE);

		int whiteColor = 0xffffffff;
		anglePaint = new Paint();
		anglePaint.setAntiAlias(true);
		anglePaint.setColor(whiteColor);
		anglePaint.setStyle(Paint.Style.FILL_AND_STROKE);

		bitmapPaint = new Paint();
		bitmapPaint.setAntiAlias(true);
		
		rectf = new RectF();

		bitmapScale = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon_round_calibration);
		setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawArc(canvas);
		drawBitmap(canvas);
	}

	private void drawArc(Canvas canvas) {
		rectf.set(0, 0, getWidth(), getWidth());
		for(int i = 0; i < 5; i++) {
			sweepAngle += 360/5;
			arcPaint.setColor(ARC_COLORS[i]);
			canvas.drawArc(rectf, startAngle, 72, true, arcPaint);
			startAngle = sweepAngle;
		}
	}

	private void drawBitmap(Canvas canvas) {
		Matrix matrix = new Matrix();
		float scale = caulateScale();
		matrix.postScale(scale, scale);
		canvas.drawBitmap(bitmapScale, matrix, bitmapPaint);
	}
	
	private float caulateScale() {
		if(bitmapScale.getWidth() > getWidth() || bitmapScale.getHeight() > getHeight()) {
			float scaleX = getWidth() * 1.0f / bitmapScale.getWidth();
			float scaleY = getHeight() * 1.0f / bitmapScale.getHeight();
			return scaleX > scaleY ? scaleY : scaleX;
		}
		return 1.0f;
	}

	/**
	private void flipVertical(Canvas canvas) {
		Matrix matrix = new Matrix();
		final float f[] = { 1.0F, 0.0F, 0.0F, 0.0F, -1.0F, 120.0F, 0.0F, 0.0F,
				1.0F };
		matrix.setValues(f);
		canvas.drawBitmap(bitmapScale, matrix, bitmapPaint);
	}
	
	private void flipVertical2(Canvas canvas) {
		Matrix matrix = new Matrix();
		matrix.setRotate(90, bitmapScale.getWidth()/2, bitmapScale.getHeight()/2);
		canvas.drawBitmap(bitmapScale, matrix, bitmapPaint);
	}
	
	private void skew(Canvas canvas) {
		Matrix matrix = new Matrix();
		matrix.setTranslate(100f,100f);
		matrix.setSkew(0.0f, 0.9f);
		canvas.drawBitmap(bitmapScale, matrix, bitmapPaint);
	}
	
	private void join(Canvas canvas) {
		Matrix matrix = new Matrix();
		float a = 100.0F,b = 100.0F; 
		matrix = new Matrix(); 
		matrix.setTranslate(a,b); 
		matrix.preRotate(30); 
		matrix.preTranslate(-a,-b);
		canvas.drawBitmap(bitmapScale, matrix, bitmapPaint);
	}
	**/
}
